#ifndef RawPackage_h__
#define RawPackage_h__

#include "../Common/Common.h"

#define MAX_PACKAGE_LEN 65533
#define SIZE_LENGTH 2
#define CMD_LENGTH 2

#define CMD_REQUEST 1
#define CMD_RESPONE 2
#define CMD_BREATHE 3

struct RawPackage
{
	RawPackage() : size_(0), buffer_(new std::string)
	{
	}

	RawPackage(const RawPackage& pk): size_(pk.size_), buffer_(new std::string)
	{
		*buffer_ = *pk.buffer_;
	}

	RawPackage(const std::string& str): buffer_(new std::string)
	{
//		CHECK(str.size() <= MAX_PACKAGE_LEN);

		size_ = static_cast<uint16>(str.size()) + SIZE_LENGTH;
		*buffer_ = str;
	}

	RawPackage(std::string* str)
	{
//		CHECK(str->size() <= MAX_PACKAGE_LEN);

		size_ = static_cast<uint16>(str->size()) + SIZE_LENGTH;
		buffer_ = str;
	}

	~RawPackage()
	{
		if (buffer_)
			delete buffer_;
	}

	size_t getExpectSize() const
	{
		return size_;
	}

	void setExpectSize(uint16 s)
	{
		size_ = s;
	}

	size_t getDataSize() const
	{
		return buffer_->size();
	}

	void clear()
	{
		size_ = 0;
		buffer_->clear();
	}

	void push(char c)
	{
		buffer_->push_back( c );
	}

	void push(const std::string& str)
	{
		buffer_ ->append( str );
	}

	std::string* getDataBuffer() const
	{
		return buffer_;
	}

	void getRawBuffer(std::string* buf) const
	{
		buf->push_back(size_ & 0xFFFF);
		buf->push_back((size_ & 0xFFFF0000) >> 8);
		buf->append(buffer_->begin(),buffer_->end());
	}

	std::string* detach()
	{
		std::string* str = buffer_;
		buffer_ = NULL;
		return str;
	}
private:
	uint16 size_;
	std::string* buffer_;
};


#endif // Package_h__